package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.newEntity.CouponMeal;
import com.tbit.uqbike.client.pojo.newEntity.UserMealLog;
import com.tbit.uqbike.client.service.CouponMealService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 优惠券套餐
 *
 * @Author: chen
 * @Description: 优惠券套餐
 * @Date: 创建于 17:23 2021/5/24
 */
@Controller
@RequestMapping("/couponMeal")
public class CouponMealController {
    @Autowired
    private CouponMealService couponMealService;
    @Autowired
    private TokenService tokenService;

    /**
     * 通过区域id获取
     *
     * @param adAccountId 区域id
     * @param session     会话
     * @param response    响应
     * @ignoreParams request
     */
    @RequestMapping("/getByAccountId")
    public void getByVipId(Integer adAccountId, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            Assert.notNull(adAccountId, "-10001", "平台Id不能为空");
            List<CouponMeal> couponMeals = couponMealService.getByAccountId(adAccountId);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, couponMeals);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 分页查询用户购买套餐记录
     *
     * @param rowCount 每页大小
     * @param pageNO   页码数
     * @param session  会话
     * @param response 响应
     * @ignoreParams request
     */
    @RequestMapping("/queryPage")
    public void queryPage(String token, Integer rowCount, Integer pageNO, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            User user = tokenService.getUser(token);
            List<UserMealLog> userMealLogs = couponMealService.queryPage(user.getUserId(), pageNO, rowCount);
            Integer total = couponMealService.queryCount(user.getUserId());
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put("total", total);
            map.put(ResponseCode.DATA, userMealLogs);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }
}
